package com.jie.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.jie.commonutils.ResultEntity;
import com.jie.servicebase.exception.GuLiException;
import com.jie.vod.service.VodService;
import com.jie.vod.utils.ConstantVodUtils;
import com.jie.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用于视频上传到阿里云视频点播的Controller类
 * @author XieZhiJie
 * @create 2020-10-13 18:10
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/eduvod/video")
public class VodController {

    private final VodService vodService;

    @Autowired
    public VodController(VodService vodService) {
        this.vodService = vodService;
    }

    /**
     * 上传视频到阿里云
     * 感觉把videoId不保存直接返回到前端有点不太妥当, 要是用户不保存, 那么视频点播
     * 那里不就会有很多没有小节指向得视频了吗? 现在回想起oss服务中好像也是这样, 或许
     * 可以先用redis保存
     * @param file  上传的视频
     * @return      返回上传是否成功
     */
    @PostMapping("/uploadAliVideo")
    public ResultEntity uploadAliVideo(
            MultipartFile file
    ) {
        // 返回上传视频的id值
        String videoId = vodService.uploadVideoAliVideo(file);
        return ResultEntity.ok().data("videoId", videoId);
    }

    /**
     * 根据视频id删除阿里云视频
     * @param id  要删除的阿里云的视频id
     * @return    返回是否成功
     */
    @DeleteMapping("/removeAliVideo/{id}")
    public ResultEntity removeAliVideo(
            @PathVariable("id") String id
    ) {
        try {
            // 初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(
                    ConstantVodUtils.ACCESS_KEY_ID,
                    ConstantVodUtils.ACCESS_KEY_SECRET
            );
            // 创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 向request设置视频id
            request.setVideoIds(id);
            // 调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return ResultEntity.ok();
        } catch(Exception e) {
            e.printStackTrace();
            throw new GuLiException(20001, "删除视频失败");
        }
    }

    /**
     * 删除多个阿里云视频的方法
     * @param videoIdList  要删除的一系列视频id
     * @return             返回是否删除成功
     */
    @DeleteMapping("/delete-batch")
    public ResultEntity deleteBatch(
            @RequestParam("videoIdList") List<String> videoIdList
            ) {
        vodService.removeMoreAlyVideos(videoIdList);
        return ResultEntity.ok();
    }

    /**
     * 根据视频id获取视频凭证
     * @param id  要获取凭证的视频的id
     * @return    返回凭证
     */
    @GetMapping("/getPlayAuth/{id}")
    public ResultEntity getPlayAuth(
            @PathVariable("id") String id
    ) {
        try {
            // 创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);

            // 创建获取凭证的request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            // 向request中设置视频id
            request.setVideoId(id);

            // 调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String auth = response.getPlayAuth();
            return ResultEntity.ok().data("playAuth", auth);
        } catch (Exception e) {
            throw new GuLiException(20001, "获取凭证失败!");
        }
    }

}
